package com.sea.supply.service.impl;

import com.sea.physical.entity.CreateSku;
import com.sea.physical.service.CreatePlanService;
import com.sea.physical.service.CreateSkuService;
import com.sea.supply.entity.*;
import com.sea.supply.mapper.DistributePlanMapper;
import com.sea.supply.service.*;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

@Service
public class DistributePlanServiceImpl implements DistributePlanService {

    @Autowired
    private DistributePlanMapper distributePlanMapper;
    @Autowired
    private DistributeSkuService distributeSkuService;
    @Autowired
    private CreateSkuService createSkuService;
    @Autowired
    private ReceiveService receiveService;
    @Autowired
    private WareDistributeService wareDistributeService;
    @Autowired
    private WareProduceService wareProduceService;



    @Override
    public List<DistributePlan> findByCnd(DistributePlan distributePlan) {
        return distributePlanMapper.findByCnd(distributePlan);
    }

    @Override
    public DistributePlan findByCreateId(Integer createId) {
        return distributePlanMapper.findByCreateId(createId);
    }

    @Override
    public int save(DistributePlan distributePlan) {
        return distributePlanMapper.insert(distributePlan);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void getDetail(Integer createId) {
        //查询发运计划明细
        List<CreateSku> list = createSkuService.findByCreateId(createId);
        DistributePlan distributePlan = findByCreateId(createId);
        if(null == list || list.size()==0){
            throw new RuntimeException("请至少导入一条明细！");
        }else {
            for(CreateSku createSku:list){
                //搜索供应商名称
                Integer number = receiveService.getDistributeNumber(createSku.getCreateSku(), createSku.getCreateChannelName(),null,null);
                Integer wareNumber = wareProduceService.getDistributeNumberBySkuChannel(createSku.getCreateSku(), createSku.getCreateChannelName());
                if((null==number || number<=0) && (null == wareNumber || wareNumber<=0)){
                    throw new RuntimeException("采购单数据不足,请补足订单!【SKU:"+createSku.getCreateSku()+";渠道:"+createSku.getCreateChannelName()+"】");
                }else{
                    List<DistributeSku> skuList = distributeSkuService.findDistributeNumber(createSku.getCreateSku(), createSku.getCreateChannelName());
                    if(null!=skuList && skuList.size()>0) {
                        for (DistributeSku distributeSku : skuList) {
                            distributeSku.setPlanId(distributePlan.getId());
                            distributeSku.setReceiveSku(createSku.getCreateSku());
                            distributeSku.setCreatePackage(createSku.getCreatePackage());
                            distributeSkuService.save(distributeSku);
                        }
                    }
                    List<WareDistribute> wareList = wareDistributeService.getDistributeNumber(createSku.getCreateSku(), createSku.getCreateChannelName());
                    if(null!=wareList && wareList.size()>0){
                        for (WareDistribute wareDistribute : wareList) {
                            DistributeSku distributeSku=new DistributeSku();
                            distributeSku.setPlanId(distributePlan.getId());
                            distributeSku.setReceiveNo(wareDistribute.getProduceNo());
                            distributeSku.setProduceSupplierName(wareDistribute.getWareName());
                            distributeSku.setReceiveSku(createSku.getCreateSku());
                            distributeSku.setReceiveNumber(wareDistribute.getReceiveNumber()-wareDistribute.getProduceUse());
                            distributeSku.setReceiveChannelName(wareDistribute.getReceiveChannelName());
                            distributeSku.setCreatePackage(createSku.getCreatePackage());
                            distributeSkuService.save(distributeSku);
                        }
                    }
                }
                updateFlag(distributePlan.getId());
            }
        }
    }

    @Override
    public int updateFlag(Integer id) {
        return distributePlanMapper.updateFlag(id);
    }


}
